<?php
/*
 * @Author: your name
 * @Date: 2021-11-02 19:06:50
 * @LastEditTime: 2022-01-17 10:19:49
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /nlog/app/Models/admin_model/Admin.php
 */

namespace App\Models\daily_partTime;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class DailyParttimeOnMessage extends Model
{
    use HasFactory;
    static string $tableName = 'daily_parttime_on_message';

    public static function add($formData){
          return DB::table(self::$tableName)->insertGetId($formData);
    }

    public static function historyList($openid,$whereStr = '',$limitStr = ''){
        //   return DB::table(self::$tableName)->where(['customer_openid'=>$openid])->get();
        return DB::select(" SELECT * FROM ".self::$tableName." WHERE `customer_openid` = '".$openid."' ".$whereStr."  ORDER BY `id` DESC ".$limitStr);
    }

    public static function messageNotRead($openid){
        return DB::select('SELECT COUNT(*) as `count` FROM `daily_parttime_on_message` WHERE `customer_openid` = "'.$openid.'" AND `is_customer` = 0 AND `is_read` = 0 ');
    }

    public static function messageCustomerRead($openid,$is_customer){
       return DB::table(self::$tableName)->where(['customer_openid'=>$openid,'is_customer'=>$is_customer])->update(['is_read'=>1]);
    }

    public static function messageReadOne($message_id){
        return DB::table(self::$tableName)->where(['id'=>$message_id])->update(['is_read'=>1]);  //阅读单条消息
    }
    

    public static function messageServiceNotRead(){
        return DB::select('SELECT COUNT(*) as `count` FROM `daily_parttime_on_message` WHERE  `is_customer` = 1 AND `is_read` = 0 ');
    }


    public static function messageServiceNotReadList($whereStr = '',$limitStr = ''){
        // echo 
        // echo '  SELECT `getMaxId`.*,`message`.`send_time` as `last_send_time`,`message`.`message` as `last_message` FROM    ( SELECT  MAX(`daily_parttime_on_message`.`id`) as `id`,SUM(CASE WHEN `daily_parttime_on_message`.`is_read` = 0 THEN 1  ELSE 0 END ) as `notRealNumber`,`daily_parttime_on_message`.`customer_openid`,`daily_parttime_user`.`nickName`,`daily_parttime_user`.`avatarUrl`  FROM `daily_parttime_on_message` LEFT JOIN `daily_parttime_user`  ON `daily_parttime_on_message`.`customer_openid` = `daily_parttime_user`.`openid`  WHERE `daily_parttime_on_message`.`is_customer` = 1 '.$whereStr.' GROUP BY `daily_parttime_on_message`.`customer_openid` ORDER BY `id` DESC '.$limitStr.' ) AS `getMaxId`  INNER JOIN `daily_parttime_on_message` AS `message` ON `message`.`id` = `getMaxId`.`id`';
        return DB::select('  SELECT `getMaxId`.*,`message`.`send_time` as `last_send_time`,`message`.`message` as `last_message` FROM    ( SELECT  MAX(`daily_parttime_on_message`.`id`) as `id`,SUM(CASE WHEN `daily_parttime_on_message`.`is_read` = 0 THEN 1  ELSE 0 END ) as `notRealNumber`,`daily_parttime_on_message`.`customer_openid`,`daily_parttime_user`.`nickName`,`daily_parttime_user`.`avatarUrl`,`daily_parttime_user`.`id` as `user_id`  FROM `daily_parttime_on_message` INNER JOIN `daily_parttime_user`  ON `daily_parttime_on_message`.`customer_openid` = `daily_parttime_user`.`openid`  WHERE `daily_parttime_on_message`.`is_customer` = 1 '.$whereStr.' GROUP BY `daily_parttime_on_message`.`customer_openid` ORDER BY `id` DESC '.$limitStr.' ) AS `getMaxId`  INNER JOIN `daily_parttime_on_message` AS `message` ON `message`.`id` = `getMaxId`.`id`');
    }

   
}
